package com.fuyun.rpc.client.config;

import com.fuyun.rpc.common.security.ClientCredentials;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * @author: zrq1351
 * @date: 2025/2/14
 */
@Data
@ConfigurationProperties(prefix = "fuyun.rpc")
public class RpcConfig {

	/**
	 * 模拟调用 返回是自定义对象 确保存在无参构造器
	 */
	private Boolean mock = false;

	/**
	 * 序列化方式
	 */
	private String serializer;

	/**
	 * 注册中心实现类
	 */
	private String registry;

	/**
	 * 负载均衡实现类
	 */
	private String loadBalancer;

	/**
	 * 重试策略实现类
	 */
	private String retryStrategy;

	/**
	 * 容错策略实现类
	 */
	private String tolerantStrategy;

	/**
	 * 协议实现类http或者tcp
	 */
	private String protocolName;

	private long timeout;

	/**
	 * 是否启用认证
	 */
	private boolean enableAuth = false;

	/**
	 * 客户端凭证
	 */
	private ClientCredentials clientCredentials;

	/**
	 * 认证实现类
	 */
	private String authenticator;

	/**
	 * 深拷贝
	 */
	public RpcConfig deepCopy() {
		RpcConfig rpcConfig = new RpcConfig();
		rpcConfig.setMock(this.mock);
		rpcConfig.setSerializer(this.serializer);
		rpcConfig.setRegistry(this.registry);
		rpcConfig.setLoadBalancer(this.loadBalancer);
		rpcConfig.setRetryStrategy(this.retryStrategy);
		rpcConfig.setTolerantStrategy(this.tolerantStrategy);
		rpcConfig.setProtocolName(this.protocolName);
		rpcConfig.setTimeout(this.timeout);
		rpcConfig.setEnableAuth(this.enableAuth);

		if (this.clientCredentials != null) {
			ClientCredentials credentials = new ClientCredentials();
			credentials.setClientId(this.clientCredentials.getClientId());
			credentials.setClientSecret(this.clientCredentials.getClientSecret());
			credentials.setTimestamp(this.clientCredentials.getTimestamp());
			credentials.setSignature(this.clientCredentials.getSignature());
			rpcConfig.setClientCredentials(credentials);
		}

		rpcConfig.setAuthenticator(this.authenticator);
		return rpcConfig;
	}

}